package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.model.SpuImage;
import com.atguigu.gmall.product.model.SpuInfo;
import com.atguigu.gmall.product.model.SpuSaleAttr;
import com.atguigu.gmall.product.service.BaseSaleAttrService;
import com.atguigu.gmall.product.service.SpuManagerService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author XIN
 * 2023/4/20
 */
@RestController
@RequestMapping("/admin/product")
public class SpuManagerController {
	@Autowired
	private SpuManagerService spuManagerService;
	@Autowired
	private BaseSaleAttrService baseSaleAttrService;
	
	/**
	 * spu分页列表
	 * @param page
	 * @param size
	 * @param category3Id
	 * @return
	 */
	@GetMapping("/{page}/{size}")
	public Result getPageSize(@PathVariable Long page, @PathVariable Long size, @RequestParam("category3Id") Long category3Id) {
		IPage<SpuInfo> iPage = new Page<>(page,size);
		iPage = spuManagerService.getPageSize(iPage,category3Id);
		return Result.ok(iPage);
	}
	
	/**
	 * 查询平台所有的销售属性名称
	 * @return
	 */
	@GetMapping("/baseSaleAttrList")
	public Result baseSaleAttrList() {
		return Result.ok(baseSaleAttrService.list());
	}
	
	/**
	 * 保存spu
	 * @param spuInfo
	 * @return
	 */
	@PostMapping("/saveSpuInfo")
	public Result saveSpuInfo(@RequestBody SpuInfo spuInfo) {
		spuManagerService.saveSpuInfo(spuInfo);
		return Result.ok();
	}
	
	/**
	 * 根据spuId 查询销售属性
	 * @param spuId
	 * @return
	 */
	@GetMapping("/spuSaleAttrList/{spuId}")
	public Result spuSaleAttrList (@PathVariable Long spuId) {
		List<SpuSaleAttr> list = spuManagerService.spuSaleAttrList(spuId);
		return Result.ok(list);
	}
	
	/**
	 * 根据商品SpuID查询当前商品所有图片
	 * @param spuId
	 * @return
	 */
	@GetMapping("/spuImageList/{spuId}")
	public Result spuImageList(@PathVariable Long spuId) {
		List<SpuImage>list = spuManagerService.spuImageList(spuId);
		return Result.ok(list);
	}
}
